package com.kjc.demo.netty.client;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

@Sharable
@Slf4j
public class ClientHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        log.info("netty client data receive:{} ",msg);
//        if (StrUtil.isNotEmpty(msg)) {
//        	int start = msg.indexOf("<?xml");
//    		String earthquakeStr = msg.substring(start);
//          	Location location = XStreamUtil.toBean(earthquakeStr, Location.class);
//          	log.info("location:{}",JSONUtil.toJsonStr(location));
//         }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.pipeline().remove(this);
        ctx.channel().close();
        ctx.close();
    }
    
    
    public static void main(String[] args) {
		String msg = "123456sdf <?xml version=\"1.0\" encoding=\"GB2312\"?>\n" + 
				"<LOCATION Version=\"1.0\">\n" + 
				"    <NETWORK Code=\"CA\" Cname=\"国家台网\" Sname=\"\"/>\n" + 
				"    <VEL_MODEL Vp_vs=\"1.710000\" Crust=\"33.000000\" Layer_num=\"3\">\n" + 
				"        <LAYER Thickness=\"15.000000\" Vel_p=\"6.010000\"/>\n" + 
				"        <LAYER Thickness=\"18.000000\" Vel_p=\"6.510000\"/>\n" + 
				"        <LAYER Thickness=\"0.000000\" Vel_p=\"8.010000\"/>\n" + 
				"    </VEL_MODEL>\n" + 
				"    <CATLOG Cata_id=\"20220726150000.00\" Event_id=\"20220726150000.00\" Operator=\"BJ\" Program=\"\" Save_time=\"20220726163853.000\" Eq_type=\"L\" O_time=\"20220726150000.000\" Lat=\"0.000000\" Lon=\"0.000000\" Depth=\"0.000000\" Ml=\"0.000000\" Ms=\"0.000000\" Md=\"\" Mb=\"\" mmB=\"\" Mw=\"\" M=\"0.0\" Dmin=\"\" Gap_azi=\"\" Rms=\"\" Erh=\"\" Erz=\"\" Qloc=\"\" Qnet=\"_sMs_\" Qcom=\"\" Sum_stn=\"\" Used_stn=\"\" Sum_pha=\"\" Used_pha=\"\" Explosion_flag=\"天然地震\" Epic_id=\"\" Source_id=\"\" Location_cname=\"测试\" Comment=\"CA\"/>\n" + 
				"</LOCATION>";
		int start = msg.indexOf("<?xml");
		String earthquakeStr = msg.substring(start);
//      	Location location = XStreamUtil.toBean(earthquakeStr, Location.class);
//      	log.info("{}", JSONUtil.toJsonStr(location));
	}
}
